/**  定义电影院相关的接口 */
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const Response = require("../utils/Response.js");

// 引入mysql连接池
const pool = require("../utils/db.js");

/**
 * 删除购物车列表接口*****************************************************************************************************************************
 * @param:
 *   car_id:   
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/cart/del", (req, resp) => {
  let { car_id, g_id ,uid} = req.body;

  if (uid && g_id) {
    // 表单验证
    let schema = Joi.object({
      g_id: Joi.number().required(), // 必填
      uid: Joi.number().required(), // 必填
    });
    let { error, value } = schema.validate(req.body);
    if (error) {
      resp.send(Response.error(400, error));
      return; // 结束
    }

    // 执行删除业务
    let sql = "delete from cart where g_id = ? and uid = ?";
    pool.query(sql, [g_id,uid], (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    });
  }else if (car_id) {
    // 表单验证
    let schema = Joi.object({
      car_id: Joi.number().required(), // 必填
    });
    let { error, value } = schema.validate(req.body);
    if (error) {
      resp.send(Response.error(400, error));
      return; // 结束
    }

    // 执行删除业务
    let sql = "delete from cart where car_id = ?";
    pool.query(sql, [car_id], (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    });
  } else if (g_id) {
    // 表单验证
    let schema = Joi.object({
      g_id: Joi.number().required(), // 必填
    });
    let { error, value } = schema.validate(req.body);
    if (error) {
      resp.send(Response.error(400, error));
      return; // 结束
    }

    // 执行删除业务
    let sql = "delete from cart where g_id = ?";
    pool.query(sql, [g_id], (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    });
  }
});

/**
 * 通过用户ID购物车列表
 * @param:
 *   car_id:   事件id   ok*******************************************************************************************************
 * @return:
 *   {code:200, msg:'ok', data:{}}
 */
router.post("/cart/query", (req, resp) => {
  let { uid
  } = req.body; // post请求参数在req.body中
  // 表单验证
  
  let schema = Joi.object({
    uid: Joi.number().required(), // 必填
  });
  let { error, value } = schema.validate(req.body);
  
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 执行查询业务
  let sql = "select * from cart where uid=?";

  pool.query(
    sql,
    [uid],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      if (result && result.length == 0) {
        // 没查到
        resp.send(Response.ok(null));
      } else {
        resp.send(Response.ok(result));
      }
    }
  );
});




/**
 * 添加购物车接口**********************************************************************************************************************************************
 * @param:
 *   详见接口文档
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/home/carts", (req, resp) => {
  let { car_id, uid, description, price, per_price, count, g_id,cover
  } = req.body; // post请求参数在req.body中

  // 表单验证
  let schema = Joi.object({
    // car_id: Joi.number().required(),
    g_id: Joi.number().required(),
    uid: Joi.number().required(),
    description: Joi.string().required(),
    cover: Joi.string().required(),
    price: Joi.number().required(),
    per_price: Joi.number().required(),
    count: Joi.number().required(),

  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }
  // 表单验证通过，执行添加操作
  let sql = `insert into cart (uid,description,price,per_price,count,g_id,cover) values (?,?,?,?,?,?,?)`;
  pool.query(
    sql,
    [uid, description, price, per_price, count, g_id,cover],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response.ok());
    }
  );
});

/**
 * 修改购物车接口******************************************************************************************************************************************
 * @param:
 *   详见接口文档
 * @return:
 *   {code:200, msg:'ok'}
 */
router.post("/cart/update", (req, resp) => {
  let {
    car_id, uid, description, price, per_price, count, g_id,cover
  } = req.body; // post请求参数在req.body中

  // 表单验证
  let schema = Joi.object({
    // car_id: Joi.number().required(),
    g_id: Joi.number().required(),
    uid: Joi.number().required(),
    description: Joi.string().required(),
    cover: Joi.string().required(),
    price: Joi.number().required(),
    per_price: Joi.number().required(),
    count: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    resp.send(Response.error(400, error));
    return; // 结束
  }

  // 表单验证通过，执行添加操作
  let sql = `update cart set     description=?,   price=?,   per_price=?,   count=? ,cover=? where uid=? and g_id=?`;
  pool.query(
    sql,
    [description, price, per_price, count,cover, uid, g_id],
    (error, result) => {
      if (error) {
        resp.send(Response.error(500, error));
        throw error;
      }
      resp.send(Response);
    }
  );
});


// 将router对象导出
module.exports = router;
